package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;

import model.Service;

public class ChangePasswordDialog extends JDialog {
	JLabel lblOld, lblNew, lblNew2, lblCombo;
	JButton btnOk, btnCancel;
	JPasswordField txfOld, txfNew, txfNew2;
	JPanel pnl, pnlSouth, pnlNorth;
	JComboBox userCom;
	DefaultComboBoxModel comboModel;

	private Service service = Service.getInstance();

	public ChangePasswordDialog(JFrame upper) {
		setTitle("Change password");
		setSize(300, 200);
		setLocation(upper.getX()+upper.getWidth(), upper.getY());
		setLayout(new BorderLayout(0,15));
		setModal(true);
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		setResizable(false);

		pnl = new JPanel(new GridLayout(3, 2, 5, 5));
		pnl.setPreferredSize(new Dimension(200, 70));
		add(pnl, BorderLayout.CENTER);
		pnlSouth = new JPanel(new GridLayout(1, 2, 10, 10));
		pnlSouth.setPreferredSize(new Dimension(200, 25));
		add(pnlSouth, BorderLayout.SOUTH);

		pnlNorth = new JPanel(new GridLayout(1, 2, 0, 10));
		pnlNorth.setPreferredSize(new Dimension(100, 25));
		add(pnlNorth, BorderLayout.NORTH);

		lblOld = new JLabel("    Type old password: ");
		lblOld.setPreferredSize(new Dimension(100, 20));
		lblNew = new JLabel("    Type new password: ");
		lblNew2 = new JLabel("    Retype new password: ");

		txfOld = new JPasswordField();
		txfOld.setPreferredSize(new Dimension(100,25));
		txfNew = new JPasswordField();
		txfNew2 = new JPasswordField();

		btnOk = new JButton("OK");
		btnCancel = new JButton("Cancel");

		comboModel = new DefaultComboBoxModel();
		comboModel.addElement("Inventory Manager");
		comboModel.addElement("Erector");
		userCom = new JComboBox(comboModel);
		lblCombo = new JLabel("    Choose your account type:");

		pnl.add(lblOld);
		pnl.add(txfOld);
		pnl.add(lblNew);
		pnl.add(txfNew);
		pnl.add(lblNew2);
		pnl.add(txfNew2);
		pnlNorth.add(lblCombo);
		pnlNorth.add(userCom);
		pnlSouth.add(btnOk);
		pnlSouth.add(btnCancel);
	
		
		btnOk.addActionListener(new Controller());
		btnCancel.addActionListener(new Controller());
	}
	
	
	public class Controller implements ActionListener {

		@SuppressWarnings("deprecation")
		public void actionPerformed(ActionEvent e) {
			String newN = txfNew.getText().trim();
			String newNA = txfNew2.getText().trim();
			String old = txfOld.getText().trim();
			String accountType = (String) userCom.getSelectedItem();
			
			if (e.getSource() == btnOk) {
				if(!service.login(accountType, old)){
					JOptionPane.showMessageDialog(ChangePasswordDialog.this, "Old password incorrect! Please retype correct password for this account type!",
																		"Old password incorrect", JOptionPane.WARNING_MESSAGE);
					txfOld.setText("");
				}
				else if(txfOld.getText().equals("") || txfNew.getText().equals("") || txfNew2.getText().equals("")) {
					JOptionPane.showMessageDialog(ChangePasswordDialog.this, "Please fill out all fields!",
							"There are still empty fields", JOptionPane.INFORMATION_MESSAGE);
				}
				else if (!newN.equals(newNA)){
					JOptionPane.showMessageDialog(ChangePasswordDialog.this, "New passwords do not match! Please retype new passwords!",
							"New passwords do not match", JOptionPane.WARNING_MESSAGE);
					txfNew.setText("");
					txfNew2.setText("");
				}
				else if((newN.equals(newNA)) && service.login(accountType, old)){
					 service.changePassword(accountType, newNA);
					 ChangePasswordDialog.this.setVisible(false);
				 }
			}
			if (e.getSource() == btnCancel) {
				ChangePasswordDialog.this.setVisible(false);
			}
		}
	}

}
